# 计算密集型
# 开启四个进程,开启四个线程
# from multiprocessing import Process
# from threading import Thread
# import time
# import os
# # print(os.cpu_count())
#
# def task1():
#     res = 1
#     for i in range(1, 100000000):
#         res += i
#
#
# def task2():
#     res = 1
#     for i in range(1, 100000000):
#         res += i
#
#
# def task3():
#     res = 1
#     for i in range(1, 100000000):
#         res += i
#
#
# def task4():
#     res = 1
#     for i in range(1, 100000000):
#         res += i
#
# if __name__ == '__main__':
#     # 四个进程 四个cpu 并行 效率
#     start_time = time.time()
#     p1 = Process(target=task1)
#     p2 = Process(target=task2)
#     p3 = Process(target=task3)
#     p4 = Process(target=task4)
#
#     p1.start()
#     p2.start()
#     p3.start()
#     p4.start()
#
#     p1.join()
#     p2.join()
#     p3.join()
#     p4.join()
#     print(f'主: {time.time() - start_time}')  # 7.53943133354187
#
#     # 一个进程 四个线程 1 cpu 并发  25.775474071502686
#     # start_time = time.time()
#     # p1 = Thread(target=task1)
#     # p2 = Thread(target=task2)
#     # p3 = Thread(target=task3)
#     # p4 = Thread(target=task4)
#     #
#     # p1.start()
#     # p2.start()
#     # p3.start()
#     # p4.start()
#     #
#     # p1.join()
#     # p2.join()
#     # p3.join()
#     # p4.join()
#     # print(f'主: {time.time() - start_time}')  # 25.775474071502686

# 计算密集型:  多进程的并行  单进程的多线程的并发执行效率高很多.


# 讨论IO密集型: 通过大量的任务去验证.
#
from multiprocessing import Process
from threading import Thread
import time
import os


# print(os.cpu_count())

def task1():
    res = 1
    time.sleep(3)


# if __name__ == '__main__':
    
    # 开启150个进程(开销大,速度慢),执行IO任务, 耗时 9.293531656265259

    # start_time = time.time()
    # l1 = []
    # for i in range(150):
    #     p = Process(target=task1)
    #     l1.append(p)
    #     p.start()
    # for i in l1:
    #     i.join()
    # print(f'主: {time.time() - start_time}')


    # 开启150个线程(开销小,速度快),执行IO任务, 耗时 3.0261728763580322
    # start_time = time.time()
    # l1 = []
    # for i in range(150):
    #     p = Thread(target=task1)
    #     l1.append(p)
    #     p.start()
    # for i in l1:
    #     i.join()
    # print(f'主: {time.time() - start_time}')  # 3.0261728763580322


# 任务是IO密集型并且任务数量很大,用单进程下的多线程效率高.